package student.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

import edu.student.models.Announcements;
import edu.student.models.News;
import edu.student.models.User;
import edu.student.models.Enrollment;
public class SelectSubject extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String semester;
	private String roll_no;
	private String commandButton = "";
	private String subject;
	private ArrayList<String> subjectlist=new ArrayList<String>();
	private ArrayList<String> semesterlist=new ArrayList<String>();
	private ArrayList<Enrollment> gradelist= new ArrayList<Enrollment>();
	private String username="";
	private List<News> news;
	private List<Announcements> announcements;
	private String image="";
	private Timestamp lastLogin;
	private String userid="";
	private float cgpa;
	public String getSemester() {
		return semester;
	}

	public void setSemester(String semester) {
		this.semester = semester;
	}
 
	public String execute() throws Exception {
		User user=new User();
		Map<String, Object> session = ActionContext.getContext().getSession();
		user = (User) session.get("user");
		
		roll_no=user.getRollNor();
		
		userid=user.getRollNor();
		username=user.getUsername();
		image=user.getImage();
		lastLogin=user.getLastLogin();
		news = News.getNews(" where ui.roll_No='"+userid+"' ");
		announcements=Announcements.getAnnouncements(" where ui.roll_No='"+userid+"' ");
		setCgpa(Grades.findCGPA(roll_no));
		
	 if(this.commandButton.startsWith("Search")){
		 semesterlist.add(semester);
		 subjectlist.add(subject);
		 gradelist= (ArrayList<Enrollment>)getGradeDetails(semester,subject,roll_no);
		 
		 return "searchresult";
		 
	 }
		if(semester.equals("none")){
			return "initial";
		}
		else {
			
			
			subjectlist=(ArrayList<String>)getSubjectlist(semester,roll_no);
			semesterlist.add(semester);
		return "success";
		}
	}

	public ArrayList<String> getSubjectlist() {
		return subjectlist;
	}

	public void setSubjectlist(ArrayList<String> subjectlist) {
		this.subjectlist = subjectlist;
	}
	ArrayList<String> getSubjectlist(String semester,String roll_no){
		ArrayList<String> selection = new ArrayList<String>();
		ResultSet result = null;
		Connection con = DB.getConnection();
		System.out.println("roll_no inside getSubjectlist = " + roll_no+ " and semester is "+ semester);
		String query= "select distinct s.subject_name from enrollment as e,subject as s,semester se where se.semester_name='"
		+ semester+ "' and e.is_enrolled='Y'and e.roll_no='"+ roll_no + 
				 "'and e.semester_id=se.semester_id and e.subject_id=s.subject_id;";
		result = DB.readFromDB(query, con);
		try {
		while (result.next()) {
		String sub;
		sub=result.getString("SUBJECT_NAME");
		System.out.println("subject is" + sub);
		selection.add(sub);
		System.out.println("in try of resultnext after addition to selection");
		}
		} catch (SQLException e) {
		      System.out.println("Exception while reading from db"+ e);
		}
		DB.close(con);
		
		return selection;
}
	ArrayList<Enrollment> getGradeDetails(String semester,String subject,String roll_no){
		ArrayList<Enrollment> selection = new ArrayList<Enrollment>();
		ResultSet result = null;
		Connection con = DB.getConnection();
		String query= "select  s.subject_name,s.subject_code,e.grade,e.result from enrollment as e,subject as s,semester as se where se.semester_name ='"+semester+"' and e.is_enrolled='Y'and s.subject_name='"
				+ subject +"' and e.roll_no='" +roll_no +"' and e.semester_id=se.semester_id and e.subject_id=s.subject_id;";
		result = DB.readFromDB(query, con);
		try {
			while (result.next()) {
			Enrollment enroll = new Enrollment();
			enroll.setSubjectName(result.getString("SUBJECT_NAME"));
			enroll.setSubjectCode(result.getString("subject_code"));
			enroll.setGrade(result.getString("GRADE"));
			enroll.setResult(result.getString("RESULT"));
			selection.add(enroll);
			System.out.println("in try of resultnext after addition to selection");
			}
			} catch (SQLException e) {
			      System.out.println("Exception while reading from db"+ e);
			}
			DB.close(con);
			
			return selection;
				
	}

	public ArrayList<String> getSemesterlist() {
		return semesterlist;
	}

	public void setSemesterlist(ArrayList<String> semesterlist) {
		this.semesterlist = semesterlist;
	}

	public String getCommandButton() {
		return commandButton;
	}

	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public ArrayList<Enrollment> getGradelist() {
		return gradelist;
	}

	public void setGradelist(ArrayList<Enrollment> gradelist) {
		this.gradelist = gradelist;
	}

	public String getRoll_no() {
		return roll_no;
	}

	public void setRoll_no(String roll_no) {
		this.roll_no = roll_no;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public List<News> getNews() {
		return news;
	}

	public void setNews(List<News> news) {
		this.news = news;
	}

	public List<Announcements> getAnnouncements() {
		return announcements;
	}

	public void setAnnouncements(List<Announcements> announcements) {
		this.announcements = announcements;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Timestamp getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Timestamp lastLogin) {
		this.lastLogin = lastLogin;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public float getCgpa() {
		return cgpa;
	}

	public void setCgpa(float cgpa) {
		this.cgpa = cgpa;
	}
	
	
}